<!--
  功能：在线预约
  作者：zzb
  邮箱：2283325110@qq.com
  时间：2021年12月06日 17:05:12
  版本：v1.0
  修改记录：
  修改内容：
  修改人员：
  修改时间：
-->
<template>
  <div class="wrap">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>新冠疫苗预约</span>
      </div>
      <el-form :model="form" label-width="80px">
        <el-form-item label="预约时间">
          <el-col :span="5">
            <el-date-picker
              v-model="form.appointDate"
              type="date"
              placeholder="选择日期"
              style="width: 100%"
            ></el-date-picker>
          </el-col>
          <el-col style="text-align: center" :span="1">-</el-col>
          <el-col :span="5">
            <el-select v-model="form.time" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="接种针剂">
          <el-select
            v-model="form.whichPin"
            placeholder="请选择"
            size="medium"
            style="width: 240px"
          >
            <el-option
              v-for="item in options2"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="接种地点">
          <el-select
            v-model="form.appointSite"
            placeholder="请选择"
            size="medium"
            style="width: 340px"
          >
            <el-option
              v-for="item in address"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="接种疫苗">
          <el-select
            v-model="form.vaccine"
            placeholder="请选择"
            size="medium"
            style="width: 340px"
          >
            <el-option
              v-for="item in vaccines"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">立即预约</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
  import { getAppointAddress, getVaccines, addAppoint } from '@/api/appoint'
  import { showUserInfo } from '@/api/userList'
  import { mapGetters } from 'vuex'

  export default {
    // 组件名称
    name: 'AppointOnline',
    // 组件参数 接收来自父组件的数据
    props: {},
    // 组件状态值
    data() {
      return {
        form: {
          appointDate: '',
          time: '',
          whichPin: '',
          appointSite: '',
          vaccine: '',
        },
        options: [
          {
            value: '上午',
            lable: '上午',
          },
          {
            value: '下午',
            lable: '下午',
          },
        ],
        options2: [
          {
            value: '第一针',
            lable: '第一针',
          },
          {
            value: '第二针',
            lable: '第二针',
          },
        ],
        address: [
          {
            value: '',
            lable: '',
          },
        ],
        vaccines: [
          {
            value: '',
            lable: '',
          },
        ],
      }
    },
    // 计算属性
    computed: {
      ...mapGetters({
        username: 'user/username',
      }),
    },
    // 侦听器
    watch: {},
    // 以下是生命周期钩子
    /**
     * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
     */
    created() {
      this.getAddress()
      this.getVaccines()
    },
    /**
     * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调用该钩子。
     * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.$ el 也在文档内。
     */
    mounted() {},
    // 组件方法
    methods: {
      onSubmit() {
        let appoint = {}
        for (const key in this.form) {
          if (this.form.hasOwnProperty.call(this.form, key)) {
            if (typeof this.form[key] == 'object') {
              let d = new Date(this.form[key]),
                month = '' + (d.getMonth() + 1),
                day = '' + d.getDate(),
                year = d.getFullYear()
              if (month.length < 2) month = '0' + month
              if (day.length < 2) day = '0' + day
              appoint[key] = [year, month, day].join('-')
            } else appoint[key] = this.form[key]
          }
        }
        showUserInfo(this.username).then((res) => {
          appoint.gender = res.data.gender
          appoint.name = res.data.realName
          appoint.userName = res.data.userName
          this.addAppointment(appoint)
        })
      },
      async addAppointment(appoint) {
        const { msg } = await addAppoint(appoint)
        this.$baseMessage(msg, 'success')
      },
      async getAddress() {
        const { data } = await getAppointAddress()
        let address = []
        data.forEach((item, index) => {
          address[index] = {}
          address[index].value = item.address
          address[index].lable = item.address
        })
        this.address = address
      },
      async getVaccines() {
        const { data } = await getVaccines()
        let vaccines = []
        data.forEach((item, index) => {
          vaccines[index] = {}
          vaccines[index].value = item.name
          vaccines[index].lable = item.name
        })
        this.vaccines = vaccines
      },
    },
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--使用了scoped属性之后，父组件的style样式将不会渗透到子组件中，-->
<!--然而子组件的根节点元素会同时被设置了scoped的父css样式和设置了scoped的子css样式影响，-->
<!--这么设计的目的是父组件可以对子组件根元素进行布局。-->
<style scoped>
  .wrap {
    /* text-align: center; */
  }
  .box-card {
    min-width: 340px;
    /* display: flex; */
  }
  .clearfix:before,
  .clearfix:after {
    display: table;
    content: '';
  }
  .clearfix:after {
    clear: both;
  }
</style>
